package com.zisu.rabbitconsumer.component;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2021/5/13 16:02
 */
//这个死信队列也是一个普通的队列，只不过外部队列把 异常消息投递到这里而已

@Component
public class DeadMessageQueueReceive {
    ////这里就是死信队列，监听死信的地方
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="es1_dead_message_queue",durable = "true"),
                    exchange = @Exchange(name = "es1_dead_message_exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "es1_eq1_dead_message"
            )
    )
    @RabbitHandler
    public void es2ExchangeEq5(Message<?> message, Channel channel)throws Exception{

        System.out.println("1当前线程名"+Thread.currentThread().hashCode()+"==============DeadMessageQueueReceive-es1_dead_message_queue=================,priority:"+message.getHeaders().get("priority")+",attr1"+message.getHeaders().get("attr1"));
        Thread.sleep(5000);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);

        //throw new Exception("12331");

        //channel.basicNack(deliveryTag,false,true);
        channel.basicAck(deliveryTag,false);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="es1_dead_message_queue2",durable = "true"),
                    exchange = @Exchange(name = "es1_dead_message_exchange2",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "es1_eq1_dead_message"
            )
    )
    @RabbitHandler
    public void es2ExchangeEq52(Message<?> message, Channel channel)throws Exception{

        System.out.println("2当前线程名"+Thread.currentThread().hashCode()+"==============DeadMessageQueueReceive-es1_dead_message_queue=================,priority:"+message.getHeaders().get("priority")+",attr1"+message.getHeaders().get("attr1"));
        Thread.sleep(5000);

        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);

        //channel.basicNack(deliveryTag,false,true);
        channel.basicAck(deliveryTag,false);
    }
}
